package org.fxml.utils {
	import org.fxml.containers.IViewStack;
	import org.fxml.display.INavigation;

	import mx.binding.utils.BindingUtils;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author jordandoczy
	 */
	public class ContentRotator implements IContentRotator {
		
		protected var _inactiveTimer:flash.utils.Timer;
		protected var _isOpen : Boolean;
		protected var _navigation:INavigation;
		protected var _timer:org.fxml.utils.Timer;
		protected var _viewStack:IViewStack;
		

		public function ContentRotator(){
			_inactiveTimer = new flash.utils.Timer(5000);
			_inactiveTimer.addEventListener(TimerEvent.TIMER, onInactiveTimer, false, 0, true);
			_inactiveTimer.start();
			
			_timer = new org.fxml.utils.Timer(1000, 0);
		}
		
		public function get inactiveTime():Number{
			return _inactiveTimer.delay;
		}
		
		public function set inactiveTime(value:Number):void{
			_inactiveTimer.delay = value;
			resetInactiveTimer();
		}
		
		public function get navigation() : INavigation {
			return _navigation;
		}
		
		public function set navigation(value : INavigation) : void {
			_navigation = value;
		}
		
		public function get rotateTime() : Number {
			return _timer.delay;
		}
		
		public function set rotateTime(value : Number) : void {
			_timer.delay = value;
		}
		
		public function get viewStack() : IViewStack {
			return _viewStack;
		}
		
		public function set viewStack(value : IViewStack) : void {
			_viewStack = value;
			_viewStack.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
			_viewStack.addEventListener(Event.OPEN, onOpen, false, 0, true);
			_viewStack.addEventListener(Event.CLOSE, onClose, false, 0, true);
			
			_timer.upperBound = _viewStack.length; 
		}
		
		public function bind():void{
			
			var selectedIndex:String = "selectedIndex";
			var index:String = "index";
			
			if(_navigation && _viewStack){
				BindingUtils.bindProperty(_viewStack, selectedIndex, _navigation, selectedIndex);
				if(_timer){
					BindingUtils.bindProperty(_navigation, selectedIndex, _timer, index);
					BindingUtils.bindProperty(_timer, index, _navigation, selectedIndex);
				}	
			}
			else if(_timer && _viewStack){
				BindingUtils.bindProperty(_viewStack, selectedIndex, _timer, index);	
			}
			
		}
		
		protected function resetInactiveTimer():void{
			_inactiveTimer.reset();
			_inactiveTimer.start();	
		}
		
		protected function resetTimer():void{
			_timer.reset();
			_timer.start();	
		}
		
		protected function onInactiveTimer(event : TimerEvent) : void {
			_timer.index++;
			_timer.start();
			_inactiveTimer.stop();
		}
		
		protected function onMouseMove(event : MouseEvent) : void {
			if(!_isOpen){
				resetInactiveTimer();
				_timer.stop();
			}
		}
		
		protected function onOpen(event : Event) : void {
			_timer.stop();
			_inactiveTimer.stop();
			_isOpen = true;
			_navigation.visible = false;
		}

		protected function onClose(event : Event) : void {
			resetInactiveTimer();
			resetTimer();
			_isOpen = false;
			_navigation.visible = true;
		}
	}
}
